package com.gxuwz.dao.impl;

import com.gxuwz.dao.LoginDao;
import com.gxuwz.entity.Instructor;
import com.gxuwz.entity.Student;
import com.gxuwz.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class LoginDaoImpl implements LoginDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public User selectUser(User entity) {
        String sql = "select * from sys_user where userId = ? and password = ? and del_flag = 0 ";
        BeanPropertyRowMapper<User> userBeanPropertyRowMapper = new BeanPropertyRowMapper<User>(User.class);
        Object[] params = new Object[]{entity.getUserId(),entity.getPassword()};
        try{
            return jdbcTemplate.queryForObject(sql,userBeanPropertyRowMapper,params);
        } catch (Exception ex){
            return null;
        }

    }

    public Student selectStudent(Student entity) {
        String sql = "select * from sys_student where stuID = ? and password = ? and del_flag = 0 ";
        BeanPropertyRowMapper<Student> studentBeanPropertyRowMapper = new BeanPropertyRowMapper<Student>(Student.class);
        Object[] params = new Object[]{entity.getStuID(), entity.getPassword()};
        try {
            return jdbcTemplate.queryForObject(sql,studentBeanPropertyRowMapper,params);
        } catch (Exception ex) {
            return null;
        }
    }

    public Instructor selectInstructor(Instructor entity) {
        String sql = "select * from sys_instructor where instID = ? and password = ? and del_flag = 0 ";
        BeanPropertyRowMapper<Instructor> instructorBeanPropertyRowMapper = new BeanPropertyRowMapper<Instructor>(Instructor.class);
        Object[] params = new Object[]{entity.getInstID(),entity.getPassword()};
        try{
            return jdbcTemplate.queryForObject(sql,instructorBeanPropertyRowMapper,params);
        } catch (Exception ex){
            return null;
        }

    }

}
